package com.cec.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.entity.User;
import com.cec.entity.Volunteer;
import com.cec.mapper.admin.ActivityRegMapper;
import com.cec.mapper.admin.UserMapper;
import com.cec.service.admin.ActivityRegService;
import com.cec.context.BaseContext;
import com.cec.dto.ActivityRegDTO;
import com.cec.entity.ActivityReg;
import com.cec.exception.OperationDataException;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.vo.ActivityRegVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 活动报名表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service
public class ActivityRegServiceImpl extends ServiceImpl<ActivityRegMapper, ActivityReg> implements ActivityRegService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result pageQueryActivityReg(ActivityRegDTO activityRegDTO) {
        QueryWrapper<ActivityRegDTO> wrapper = new QueryWrapper<>();
        if (activityRegDTO.getActivityName() != null && !activityRegDTO.getActivityName().equals(""))
            wrapper.like("a.activity_name", activityRegDTO.getActivityName());
        if (activityRegDTO.getUserName() != null && !activityRegDTO.getUserName().equals(""))
            wrapper.like("u.user_name", activityRegDTO.getUserName());
        if (activityRegDTO.getStatus() != null)
            wrapper.eq("ar.status", activityRegDTO.getStatus());
        wrapper.eq("ar.deleted",0).orderByDesc("ar.create_time");
        IPage<ActivityRegVO> activityRegVOIPage = baseMapper.pageQueryActivityReg(new Page<>(activityRegDTO.getPage(), activityRegDTO.getPageSize()), wrapper);
        log.info("activityVOIPage.getRecords()-->{}", activityRegVOIPage.getRecords());
        log.info("activityVOIPage.getTotal()-->{}", activityRegVOIPage.getTotal());
        return Result.success(activityRegVOIPage.getRecords(), activityRegVOIPage.getTotal());
    }

    @Override
    public Result updateActivityReg(ActivityRegDTO activityRegDTO) {

        ActivityReg activityReg = baseMapper.selectById(activityRegDTO.getId());
        User user = userMapper.selectById(activityReg.getUserId());
        if (user!=null && activityRegDTO.getServiceDuration()!=0){
            Double serviceDuration = user.getServiceDuration();
            user.setServiceDuration(serviceDuration + activityRegDTO.getServiceDuration());
            userMapper.updateById(user);
        }

        if (user!=null && activityRegDTO.getCreditScore()!=0){
            User setUserCreditScore = user.setCreditScore(activityRegDTO.getCreditScore());
            userMapper.updateById(setUserCreditScore);
        }

        UpdateWrapper<ActivityReg> updateWrapper = new UpdateWrapper<>();
        if (activityRegDTO.getStatus() != null && activityRegDTO.getStatus()!=-1)
            updateWrapper.set("status", activityRegDTO.getStatus());
        if (activityRegDTO.getOpinion()!=null && !"".equals(activityRegDTO.getOpinion()))
            updateWrapper.set("opinion", activityRegDTO.getOpinion());
        if (activityRegDTO.getEvaluate()!=null && !"".equals(activityRegDTO.getEvaluate()))
            updateWrapper.set("evaluate",activityRegDTO.getEvaluate());
        updateWrapper.eq("id", activityRegDTO.getId());
        int i = baseMapper.update(new ActivityReg(), updateWrapper);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicDeleteActivityRegById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicBatchDeleteActivityRegByIds(List<Integer> ids) {
        int i = baseMapper.deleteBatchIds(ids);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }
}
